home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / adg_4_6.zip / CNTL-DE.C next >
C/C++ Source or Header  |  1991-02-21  |  8KB  |  217 lines

  1. /****************************************************************************
  2. Module name: Cntl-DE.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************************/
  5.  
  6. #include "..\nowindws.h"
  7. #undef NOCTLMGR
  8. #undef NOKERNEL
  9. #undef NOLSTRING
  10. #undef NOMEMMGR
  11. #undef NOUSER
  12. #include <windows.h>
  13. #include <custcntl.h>
  14.  
  15. #include "cntl-de.h"
  16.  
  17.  
  18. // Property string used internally to store local handle of CTLSTYLEDLG
  19. // data structure.
  20. static char _szCtlProp[] = "CtlDlgStyleData";
  21.  
  22.  
  23. // Data structure used internally to get information into the style
  24. // dialog box function.
  25. typedef struct {
  26.    GLOBALHANDLE hCtlStyle;    // Memory handle holds CTLSTYLE for control.
  27.    LPFNSTRTOID  lpfnStrToId;  // DIALOG func to cnvrt string ID to number.
  28.    LPFNIDTOSTR  lpfnIdToStr;  // DIALOG func to cnvrt numeric ID to string.
  29. } CTLSTYLEDLG, FAR *LPCTLSTYLEDLG, NEAR *NPCTLSTYLEDLG;
  30.  
  31.  
  32. // This function should be called first in the ClassInfo function to 
  33. // initialize the new control.
  34. GLOBALHANDLE FAR PASCAL ControlInfo (WORD wVersion, LPSTR szClass, LPSTR szTitle) {
  35.    GLOBALHANDLE hMem = NULL;
  36.    LPCTLINFO lpCtlInfo;
  37.  
  38.    hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
  39.                         (DWORD) sizeof(CTLINFO));
  40.    if (hMem == NULL) return(hMem);
  41.    lpCtlInfo = (LPCTLINFO) GlobalLock(hMem);
  42.    lpCtlInfo->wVersion  = wVersion;
  43.  
  44.    // Initialize wCtlTypes to zero, incremented by AddControlType function.
  45.    lpCtlInfo->wCtlTypes = 0;
  46.    lstrcpy(lpCtlInfo->szClass, szClass);
  47.    lstrcpy(lpCtlInfo->szTitle, szTitle);
  48.    GlobalUnlock(hMem);
  49.    return(hMem);
  50. }
  51.  
  52.  
  53.  
  54. // This function should be called repeatedly to add new control types to
  55. // the structure returned by the ControlInfo function.  This function should
  56. // be called in the ClassInfo function.
  57. BOOL FAR PASCAL AddControlType (GLOBALHANDLE hMem, WORD wType,
  58.             WORD wWidth, WORD wHeight, DWORD dwStyle, LPSTR szDescr) {
  59.    LPCTLINFO lpCtlInfo; WORD wNumTypes;
  60.    lpCtlInfo = (LPCTLINFO) GlobalLock(hMem);
  61.    wNumTypes = lpCtlInfo->wCtlTypes;
  62.    if (wNumTypes == CTLTYPES) {
  63.       GlobalUnlock(hMem);
  64.       return(FALSE);
  65.    }
  66.    lpCtlInfo->Type[wNumTypes].wType   = wType;
  67.    lpCtlInfo->Type[wNumTypes].wWidth  = wWidth;
  68.    lpCtlInfo->Type[wNumTypes].wHeight = wHeight;
  69.    lpCtlInfo->Type[wNumTypes].dwStyle = dwStyle;
  70.    lstrcpy(lpCtlInfo->Type[wNumTypes].szDescr, szDescr);
  71.    lpCtlInfo->wCtlTypes++;
  72.    GlobalUnlock(hMem);
  73.    return(TRUE);
  74. }
  75.  
  76.  
  77.  
  78. // This function displays the control's style dialog box and should be called
  79. // from the ClassStyle function.
  80. int FAR PASCAL ShowStyleDlg (HANDLE hInstance, LPSTR szTemplate,
  81.                               HWND hWndParent, FARPROC fpDlgProc, LONG lParam,
  82.                               GLOBALHANDLE hCtlStyle, LPFNSTRTOID lpfnStrToId,
  83.                               LPFNIDTOSTR lpfnIdToStr) {
  84.    LOCALHANDLE hCtlStyleDlg;
  85.    NPCTLSTYLEDLG npCtlStyleDlg;
  86.    int x;
  87.  
  88.    hCtlStyleDlg =
  89.       LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, sizeof(CTLSTYLEDLG));
  90.    if (hCtlStyleDlg == NULL) return(FALSE);
  91.  
  92.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  93.    npCtlStyleDlg->hCtlStyle = hCtlStyle;
  94.    npCtlStyleDlg->lpfnStrToId = lpfnStrToId;
  95.    npCtlStyleDlg->lpfnIdToStr = lpfnIdToStr;
  96.    LocalUnlock(hCtlStyleDlg);
  97.  
  98.    // Associate property with Dialog Editor's window.
  99.    SetProp(hWndParent, _szCtlProp, hCtlStyleDlg);
  100.  
  101.    // Display control's Styles Dialog Box.
  102.    x = DialogBoxParam(hInstance, szTemplate, hWndParent, fpDlgProc, lParam);
  103.  
  104.    // Remove property associated with Dialog Editor's window.
  105.    RemoveProp(hWndParent, _szCtlProp);
  106.  
  107.    LocalFree(hCtlStyleDlg);
  108.    return(x == IDOK);   // Return whether CTLSTYLE structure has been changed.
  109. }
  110.  
  111.  
  112.  
  113. // This function should only be called from the ClassDlgFn function.  It 
  114. // locks the memory block containing the CTLSTYLE structure for the selected 
  115. // control and returns the FAR address to that structure.
  116. LPCTLSTYLE FAR PASCAL CtlStyleLock (HWND hDlg) {
  117.    LOCALHANDLE hCtlStyleDlg;
  118.    NPCTLSTYLEDLG npCtlStyleDlg;
  119.    LPCTLSTYLE lpCtlStyle = NULL;
  120.  
  121.    // Property is associated with Dialog Editor's window.  Parent of 
  122.    // the dialog box is the Dialog Editor.
  123.    hCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  124.  
  125.    if (hCtlStyleDlg == NULL) return(lpCtlStyle);
  126.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  127.    lpCtlStyle = (LPCTLSTYLE) GlobalLock(npCtlStyleDlg->hCtlStyle);
  128.    LocalUnlock(hCtlStyleDlg);
  129.    return(lpCtlStyle);
  130. }
  131.  
  132.  
  133.  
  134. // This function should only be called from the ClassDlgFn function.  It 
  135. // unlocks the memory block containing the CTLSTYLE structure for the 
  136. // selected control and returns whether the block was successfully unlocked.
  137. BOOL FAR PASCAL CtlStyleUnlock (HWND hDlg) {
  138.    LOCALHANDLE hCtlStyleDlg;
  139.    NPCTLSTYLEDLG npCtlStyleDlg;
  140.    BOOL fOk = FALSE;
  141.  
  142.    // Property is associated with Dialog Editor's window.  Parent of 
  143.    // the dialog box is the Dialog Editor.
  144.    hCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  145.  
  146.    if (hCtlStyleDlg == NULL) return(fOk);
  147.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  148.    fOk = GlobalUnlock(npCtlStyleDlg->hCtlStyle);
  149.    LocalUnlock(hCtlStyleDlg);
  150.    return(fOk);
  151. }
  152.  
  153.  
  154.  
  155. // This function should only be called from the ClassDlgFn function.  It 
  156. // converts the ID value for the control into a identifier string and stores
  157. // the string in the address passed in.  The number of characters in the 
  158. // string is returned.
  159. WORD FAR PASCAL GetIdString (HWND hDlg, LPSTR szId, WORD wIdMaxLen) {
  160.    LOCALHANDLE hCtlStyleDlg;
  161.    NPCTLSTYLEDLG npCtlStyleDlg;
  162.    LPCTLSTYLE lpCtlStyle;
  163.    WORD wIdLen;
  164.  
  165.    // Property is associated with Dialog Editor's window.  Parent of 
  166.    // the dialog box is the Dialog Editor.
  167.    hCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  168.    if (hCtlStyleDlg == NULL) return(0);
  169.  
  170.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  171.    lpCtlStyle = (LPCTLSTYLE) GlobalLock(npCtlStyleDlg->hCtlStyle);
  172.  
  173.    // Call the lpfnIdToStr function to convert the numeric ID to its 
  174.    // string equivalent.
  175.    wIdLen = (*npCtlStyleDlg->lpfnIdToStr)(lpCtlStyle->wId, szId, wIdMaxLen);
  176.  
  177.    GlobalUnlock(npCtlStyleDlg->hCtlStyle);
  178.    LocalUnlock(hCtlStyleDlg);
  179.    return(wIdLen);
  180. }
  181.  
  182.  
  183.  
  184.  
  185. // This function should only be called from the ClassDlgFn function.  It 
  186. // converts an ID string value into its numeric equivalent and stores the
  187. // numeric value in the CTLSTYLE structure for the control.  If the loword of
  188. // the result is 0, the ID is invalid, otherwise, the hiword contains the
  189. // numeric value of the ID.
  190. DWORD FAR PASCAL SetIdValue (HWND hDlg, LPSTR szId) {
  191.    LOCALHANDLE hCtlStyleDlg;
  192.    NPCTLSTYLEDLG npCtlStyleDlg;
  193.    LPCTLSTYLE lpCtlStyle;
  194.    DWORD dwResult = 0;
  195.  
  196.    hCtlStyleDlg = GetProp(GetParent(hDlg), _szCtlProp);
  197.    if (hCtlStyleDlg == NULL) return(dwResult);
  198.  
  199.    npCtlStyleDlg = (NPCTLSTYLEDLG) LocalLock(hCtlStyleDlg);
  200.  
  201.    // Call the lpfnStrToId function to convert the string ID to its
  202.    // numeric equivalent.
  203.    dwResult = (*npCtlStyleDlg->lpfnStrToId)(szId);
  204.  
  205.    LocalUnlock(hCtlStyleDlg);
  206.  
  207.    // If LOWORD is zero, string NOT found.
  208.    if (LOWORD(dwResult) == 0)
  209.       return(dwResult);
  210.  
  211.    // LOWORD is not zero, numeric ID is in the HIWORD.
  212.    lpCtlStyle = CtlStyleLock(hDlg);
  213.    lpCtlStyle->wId = HIWORD(dwResult);
  214.    CtlStyleUnlock(hDlg);
  215.    return(dwResult);
  216. }
  217.